package com.itheima.reggie.controller;

import com.itheima.reggie.commons.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Page;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Api(tags ="菜品分类")
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController extends BaseController{
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }
    @GetMapping("page")
    public R<Page<Category>> findByPage(@RequestParam("page") Integer pageNum, Integer pageSize){
        Page<Category> byPage = categoryService.findByPage(pageNum, pageSize);
        return R.success(byPage);
    }
    @DeleteMapping
    public R<String> deleteById(Long id){
        categoryService.deleteById(id);
        return R.success("删除成功");
    }
    /**
     * 作用：修改类别
     * @param category
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.update(category);
        return R.success("修改成功");
    }

    @SneakyThrows
    @GetMapping("/exportExcel")
    public void exportExcel(){
        //指定响应类型为application/vnd.ms-excel格式
        response.setContentType("application/vnd.ms-excel");

        //以下载的附件的方式打开，即浏览器出现下载对话框
        response.setHeader("content-disposition","attachment;filename=category.xlsx");

        //1. 读取模板的输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/category_template.xlsx");

        //2. 使用模板的输入流构建一个工作薄
        Workbook workbook = new XSSFWorkbook(inputStream);

        //3. 得到第0个工作表
        Sheet sheet = workbook.getSheetAt(0);

        //4. 读取第2行,提取第2行的样式，并且存储到集合中(不同背景色的那一行)
        Row row = sheet.getRow(1);
        List<CellStyle> cellStylesList= new ArrayList<>();
        //一共5列
        for (int i = 0; i < 5; i++) {
            //得到5列的单元格的样式，添加到集合中
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStylesList.add(cellStyle);
        }

        //5. 调用业务层得到数据,遍历数据，把数据写入到excel
        List<Category> categoryList = categoryService.findAll(null);
        for (int i = 0; i < categoryList.size(); i++) {
            //从第二行开始填写
            row =  sheet.createRow(i+1);
            Category category = categoryList.get(i);
            //把类别信息设置到单元格上

            //类别类型
            Cell cell = row.createCell(0);
            //设置单元格内容
            cell.setCellValue(category.getType());
            cell.setCellStyle(cellStylesList.get(0));

            //类别名字
            cell = row.createCell(1);
            //设置单元格内容
            cell.setCellValue(category.getName());
            cell.setCellStyle(cellStylesList.get(1));

            //类别排序
            cell = row.createCell(2);
            //设置单元格内容
            cell.setCellValue(category.getSort());
            cell.setCellStyle(cellStylesList.get(2));

            //创建时间
            cell = row.createCell(3);
            //设置单元格内容
            cell.setCellValue(category.getCreateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(3));

            //创建时间
            cell = row.createCell(4);
            //设置单元格内容
            cell.setCellValue(category.getUpdateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(4));
        }
        //6. 把工作薄写出
        workbook.write(response.getOutputStream());
    }

    @SneakyThrows
    @GetMapping("/list")
    public R<List<Category>> findAll(Integer type){
        List<Category> categoryList = categoryService.findAll(type);
        return R.success(categoryList);
    }
}
